package com.htz.java250805_library.controller;

import com.htz.java250805_library.constants.Constants;
import com.htz.java250805_library.entity.BookInfo;
import com.htz.java250805_library.entity.PageRequest;
import com.htz.java250805_library.entity.PageResult;
import com.htz.java250805_library.entity.Result;
import com.htz.java250805_library.enums.ResultCode;
import com.htz.java250805_library.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping(value = "/getList",produces = "application/json;charset=utf-8")
    public List<BookInfo> getList(){
        BookService bookService = new BookService();
        List<BookInfo> bookInfos = bookService.getList();
        return bookInfos;
    }

    @RequestMapping(value = "/addBook", produces = "application/json;charset=utf-8")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, bookInfo: {}",bookInfo);
        //参数校验
        //添加图书
        if (!StringUtils.hasLength(bookInfo.getBookName())
            ||!StringUtils.hasLength(bookInfo.getAuthor())
            ||bookInfo.getCount() == null
            ||bookInfo.getPrice() == null
            ||!StringUtils.hasLength(bookInfo.getPublish())
            ||bookInfo.getStatus() == null){
            return "参数不合法，请重新检查";
        }
        Integer result =  bookService.addBook(bookInfo);
        return result == 1 ? "" : "图书插入失败";
    }

    @RequestMapping(value = "/getListByPage",produces = "application/json;charset=utf-8")
    public Result<PageResult> getListByPage(PageRequest pageRequest, HttpSession session){
        //判断用户是否登录
        String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
        if (!StringUtils.hasLength(userName)){
            //说明未登录
            return Result.unlogin();
        }
        log.info("获取图书列表,pageRequest:{}",pageRequest);
        PageResult pageResult = bookService.getListByPage(pageRequest);

        return Result.success(pageResult);
    }

    @RequestMapping(value = "/queryBookById",produces = "application/json;charset=utf-8")
    public BookInfo queryBookById(Integer bookId){
        log.info("查询图书详情,bookId:"+bookId);
        if (bookId==null || bookId<=0){
            return null;
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookInfo;
    }

    @RequestMapping( value = "/updateBook",produces = "application/json;charset=utf-8")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书,bookInfo:{}",bookInfo);
        //参数校验
        if (bookInfo.getId()<=0){
            return "参数不正确，请重新检查";
        }
        Integer result = bookService.updateBookById(bookInfo);
        return result == 1 ? "" : "图书更新失败";
    }

    @RequestMapping(value = "/deleteBook",produces = "application/json;charset=utf-8")
    public String deleteBook(Integer bookId){
        log.info("更新图书,bookInfo:{}",bookId);
        //参数校验
        if (bookId<=0){
            return "参数不正确，请重新检查";
        }
        Integer result = bookService.deleteBook(bookId);
        return result == 1 ? "" : "图书删除失败";
    }

    @RequestMapping(value = "/batchDeleteBook",produces = "application/json;charset=utf-8")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书，ids");
        try{
            bookService.batchDeleteBook(ids);
            return "";
        }catch (Exception e){
            log.error("批量删除失败，e:" + e);
            return "批量删除失败";
        }
    }
}


































